package com.efast.dmom.mgmt.service.mes.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mgmt.bean.mes.HomePageReportQueryBean;
import com.efast.dmom.mgmt.dao.mes.HomePageReportQueryDao;
import com.efast.dmom.mgmt.service.mes.HomePageReportQueryService;

@Service
public class HomePageReportQueryServiceImpl implements HomePageReportQueryService {

	@Autowired
	private HomePageReportQueryDao homePageReportQueryDao;
	
	@Override
	public List<HomePageReportQueryBean> planAndActualCountQuery() {
		//计划完成数量
		List<HomePageReportQueryBean> planList=homePageReportQueryDao.selectWeekPlanQty();
		//实际完成数量
		List<HomePageReportQueryBean> actualList=homePageReportQueryDao.selectWeekActualQty();
		List<HomePageReportQueryBean> planQtyAndActualQtyList = new ArrayList<HomePageReportQueryBean>();
		List<HomePageReportQueryBean> returnList = new ArrayList<HomePageReportQueryBean>();
		if(planList!=null&&planList.size()>0&&actualList!=null&&actualList.size()>0) {
         for(HomePageReportQueryBean bean:planList) {
        	 Boolean flag = true;
        	 for(HomePageReportQueryBean data:actualList) {
        		if(bean.getWeekTime() == data.getWeekTime()) {
        			HomePageReportQueryBean homePageReportQueryBean=new HomePageReportQueryBean();
        			homePageReportQueryBean.setPlanQty(bean.getPlanQty());
        			homePageReportQueryBean.setWeekTime(bean.getWeekTime());
        			homePageReportQueryBean.setActualQty(data.getActualQty());
        			planQtyAndActualQtyList.add(homePageReportQueryBean);
        			flag = false;
        			break;
        		} 
        	 }
        	 if(flag) {
        	  planQtyAndActualQtyList.add(bean);
        	 }
         }
         if(planQtyAndActualQtyList!=null && planQtyAndActualQtyList.size()>0) {
        	for(HomePageReportQueryBean data:actualList) {
        	  Boolean flag = true;	
        	 for(HomePageReportQueryBean qtyData:planQtyAndActualQtyList) {
        		 if(data.getWeekTime() == qtyData.getWeekTime()) {
        			flag = false;
        			break;
        		 }
        	 }
        	 if(flag) {
        	 planQtyAndActualQtyList.add(data); 
        	 }
          }
        	Collections.sort(planQtyAndActualQtyList, new Comparator<HomePageReportQueryBean>() {
                public int compare(HomePageReportQueryBean o1, HomePageReportQueryBean o2) {
                    int diff = o1.getWeekTime() - o2.getWeekTime();
                    if(diff > 0)      return 1;
                    else if(diff < 0) return  -1;
                                      return 0;//相等返回0

         }
		});
         }
         if (planQtyAndActualQtyList.size() >= 10) {
        	 for (int i=planQtyAndActualQtyList.size()-10;i < planQtyAndActualQtyList.size();i++) {
            	 returnList.add(planQtyAndActualQtyList.get(i));
            } 
         } else {
        	 for(HomePageReportQueryBean data:planQtyAndActualQtyList) {
        		 returnList.add(data);
        	 }
         }
		}
		return returnList;
	}

	@Override
	public List<HomePageReportQueryBean> topQualityProblemQuery() {
		List<HomePageReportQueryBean> list=new ArrayList<HomePageReportQueryBean>();
		List<HomePageReportQueryBean> topQualityProblemQueryList=homePageReportQueryDao.topQualityProblemQuery();
		List<String> typeList =homePageReportQueryDao.selectTopType();
		for(String type : typeList) {
			for(int i=0; i<7; i++) {
				HomePageReportQueryBean queryBean = new HomePageReportQueryBean();
				boolean found = false;
				for(HomePageReportQueryBean bean : topQualityProblemQueryList) {
					if(bean.getWeekDay() != null && bean.getWeekDay() == i && type.equals(bean.getType())) {
						found = true;
						queryBean.setQty(bean.getQty());
						queryBean.setType(type);
						queryBean.setWeekDay(i);
						break;
					}
				}
				if(! found) {
					queryBean.setQty(0);
					queryBean.setType(type);
					queryBean.setWeekDay(i);
				}
				list.add(queryBean);
			}
		}
		 int id = 0;
		for(String type : typeList) {
			id=id+1;
			for(HomePageReportQueryBean list1:list)
			if(list1.getType().equals(type)) {
				list1.setTypeFlag(id);
			}
		}
		return list; 
	}

}
